<template>
	<page-meta :page-style="'overflow:'+(popShow?'hidden':'visible')"></page-meta>
	<view class="money-content">
		<headassy title="应收应付" searchTitle="工单单号" @goSearch="goSearch" @openSideBar="getAllSearch" :isTab="false"></headassy>
		<view class="nav-status text-orange">
			当前状态有{{count}}个报表
		</view>
		<scroll-view>
			<view class="card-list">
				<view class="card mt-5" v-for="item in pageList" :key="item.id">
					<view class="top u-flex-between">
						<view class="left u-flex">
							<view class="text-info ">
								{{item.workNo}}
							</view>
						</view>
						<view class="right">
							<view class="cus-btn">
								{{item.receivePayStatus }}
							</view>
						</view>
					</view>
					<view class="center">
						<view class="title u-flex-between">
							<view class="title-left text-gray">
								收付款编号:
								<span style="margin-left: 14rpx;">{{item.number}}</span>
							</view>
						</view>
						<view class="text">
							客户/网点:
							<span style="margin-left: 14rpx;">{{item.partnerName}}</span>
						</view>
						<view class="text">
							收付金额:
							<span style="margin-left: 14rpx;">{{item.money}}</span>
						</view>
						<view class="text">
							收付类型:
							<span style="margin-left: 14rpx;">{{item.receivePayType}}</span>
						</view>
						<view class="text">
							创建时间:
							<span style="margin-left: 14rpx;">{{item.createDate}}</span>
						</view>

					</view>
					<view class="down u-flex-content-end">
					<!-- 	<view class="right u-flex">
							<view class="cus-btn text-blue mr-5">
								配件费:{{item.spareFee}}
								<span style="color: red;">¥</span>
							</view>
							<view class="cus-btn text-blue mr-5">
								合计:{{item.workFee}}
								<span style="color: red;">¥</span>
							</view>
						</view> -->
					</view>
				</view>
			</view>
			<view class="example-body">
				<uni-load-more :status="status" :content-text="contentText"/>
			</view>
		</scroll-view>
		
		<!-- 侧边栏 -->
		<sidebarpop
			ref="page-sidebar"
			ptitle="筛选"
			isSlot="true"
			:height="(parseInt(system.safeArea.top) + 44)"
			:sidebarList="JSON.stringify(sidebarList)"
			@closeclick="closePop"
			@radioClick="radioClick"
			@clearSelect="clearSelect"
			@saveclick="saveSelect"
		>
		</sidebarpop>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				count: '',
				
				page: {
					pageNum: 1,
					pageSize: "20",
					custAccId: '',
					searchType: "3",  		// 搜索类型  1：客户、2：网点，3：工单单号
				},
				data: {
					receivePayStatus: "",	// 收付款状态 1：待收款，2：待付款，3：已收款，4：已付
					receivePayType: "",		// 收付类型 1：服务费，2：配件费
					search: "",	
				},
				pageList: [],				// 页面数据
				
				system: uni.getSystemInfoSync(),
				
				// 侧边栏
				sidebarList: [],			// 侧边栏数据
				popShow: false,				// 弹窗触发时为true
				popShowName: '',			// 弹窗名称
				
				// 搜索项列表
				receivePayStatusList: [],	// 收付类型
				receivePayTypeList: [],		// 服务类型
				
				// 加载动作
				status: 'loading',			//上拉加载
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			};
		},
		onLoad() {
			this.page.custAccId = uni.getStorageSync('custAccid')
			this.getPullRefresh();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getPullRefresh()
		},
		// 触底监听
		onReachBottom() {
			if(this.status != 'noMore') {
				return 
			}
			console.log('触底，列表分组总数:'+Math.ceil(this.count / 20),'当前列表:'+this.page.pageNum);
			if(this.page.pageNum < Math.ceil(this.count / 20)) {
				this.page.pageNum++;
				this.getPageList('refresh');
			}
		},
		methods: {
			// 下拉刷新请求数据
			getPullRefresh(){
				console.clear()
				setTimeout(()=>{
					uni.stopPullDownRefresh();
				}, 1500);
				this.page.pageNum = 1;
				this.getSelectList();
				this.getPageList();
			},
			async getPageList(type) {
				if(type != 'refresh') {
					this.pageList = [];
					this.count = 0;
					this.status = 'loading';
				}
				const res = await uni.$api.money.getReceivePayFnanceList(this.page.custAccId, {
					...this.page,
					...this.data,
				})
				if(type == 'refresh') {
					this.pageList.push(...res.data.rows);
				}else {
					this.pageList = res.data.rows
				}
				this.status = 'noMore';
				this.count = res.data.count
				uni.stopPullDownRefresh()
			},
			// 获取搜索项列表
			async getSelectList() {
				// 收付类型 
				this.receivePayStatusList = [
					{ischeck: false,title: '收款单',value: '1'},
					{ischeck: false,title: '付款单',value: '2'},
					{ischeck: false,title: '全部收付类型',value: ''},
				];
				//服务类型
				this.receivePayTypeList = [
					{ischeck: false,title: '服务费',value: '1'},
					{ischeck: false,title: '配件费',value: '2'},
					{ischeck: false,title: '全部类型',value: ''},
				]
			},
			
			// 初始化侧边栏数据
			sidebarInit() {
				const sidebarLable = ['收付类型','服务类型'];
				const sidebarList = [this.receivePayStatusList,this.receivePayTypeList];
				this.sidebarList = [];
				sidebarLable.forEach((val,index) => {
					this.sidebarList.push({
						selectEle: null,
						isShowList: false,
						title: val,
						isModelShow: true,
						data: this.handleSidebarData(sidebarList[index])
					});
				})
				
			},
			handleSidebarData(res = []) {
				let data = res;
				data.forEach(val => {
					val.ischeck = false;
					if(!val.title) {
						if(val.nameVal) {
							val.title = val.nameVal
						}
						if(val.name) {
							val.title = val.name
						}
					}
				})
				return data
			},
			// 侧边栏模块标签点击
			radioClick(item,index,isRepeat) {
				console.log('侧边栏模块标签点击----',item,index,isRepeat)
				switch (index) {
					case 0: // 收付类型
						this.data.receivePayStatus = isRepeat ? '' : item.value;
						break;
					case 1: // 服务类型
						this.data.receivePayType = isRepeat ? '' : item.value;
						break;
				}
			},
			// 确认筛选
			saveSelect() {
				this.page.pageNum = 1;
				this.getPageList();
				this.closePop();
			},
			// 清除数据
			clearSelect() {
				Object.keys(this.data).forEach(key => {
					this.data[key] = key != 'serviceId' ? '' : 1
				})
			},
			
			// 搜索
			goSearch(e) {
				this.data.search = e;
				this.saveSelect();
			},
			
			// 打开侧边栏
			getAllSearch() {
				this.openModel('page-sidebar');
				setTimeout(() => {
					this.sidebarInit();
				})
			},
			// 新开弹窗
			openModel(ref) {
				if(this.$refs[ref]) {
					this.popShow = true;
					this.popShowName = ref;
					this.$refs[ref].pupshow = true;
				}
			},
			// 关闭弹窗
			closePop() {
				this.popShowName = '';
				this.popShow = false;
			},
		},

	}
</script>

<style lang="scss">
	// 去掉顶栏固定
	/deep/.u-navbar--fixed {
		position: static !important;
	}
	// 去掉默认背景色
	/deep/.u-popup__content {
		background-color: transparent !important;
	}
	
	.money-content {
		// height: 100vh;
		background-color: $uni-bg-color-grey;

		.tabs {
			background-color: #fff;
			font-size: 28rpx;
			width: 100%;
		}

		.nav-status {
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 28rpx;
		}

		.card-list {
					background: $uni-bg-color-grey;
		padding: 0 28rpx;
					.card {
						padding: 28rpx 32rpx;
						background: #fff;
		border-radius: 10rpx;
						margin-bottom: 28rpx;
				.top {
					border-bottom: solid 1px #f5f5f5;
					padding-bottom: 10px;

					.left {
						.text-info {
							font-size: 28rpx;
							font-weight: 700;
						}
					}
				}

				.center {
					padding-top: 10px;
					padding-bottom: 10px;
				
					.title {
						.title-left {
							font-size: 28rpx;
							color: gray;
						}

						.title-right {
							// font-size: 28rpx;
						}
					}

					.text {
						font-size: 28rpx;
						color: gray;
					}
				}

				.down {
					border-top: solid 1px #f5f5f5;
					padding-top: 12px;
				}
			}
		}
	}
</style>